package net.esj.basic.pojo.op;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.esj.basic.pojo.op.Operation.OperationTarget;

public class OperationSet {

	private Set<Operation> operations = new HashSet<Operation>();
	
	public void push(Operation oper){
		if(!checkExist(oper)){
			operations.add(oper);
		}
	}
	
	public Collection<Operation> operations(String target){
		List<Operation> list = new ArrayList<Operation>();
		for(Operation op : operations){
			if(org.apache.commons.lang.StringUtils.equals(op.getTarget(), target)){
				list.add(op);
			}
		}
		Collections.sort(list, new Comparator<Operation>() {

			@Override
			public int compare(Operation o1, Operation o2) {
				return o1.getSort()-o2.getSort();
			}
		});
		return Collections.unmodifiableCollection(list);
	}
	
	public boolean hasGlobal(){
		for(Operation op : operations){
			if(OperationTarget.Global== OperationTarget.valueOf(op.getTarget())){
				return true;
			}
		}
		return false;
	}
	
	public boolean hasContextMenu(){
		for(Operation op : operations){
			if(OperationTarget.ContextMenu== OperationTarget.valueOf(op.getTarget())){
				return true;
			}
		}
		return false;
	}
	
	protected boolean checkExist(Operation oper){
		for(Operation op : operations){
			if(op.equals(oper)){
				return true;
			}
		}
		return false;
	}
	
	public static final OperationSet EMPTY = new OperationSet();

}

